package customerEnter.Frame.JDBCDao.SqlExecutive;

import customerEnter.Frame.DaoDataTransmission.DaoFramePameTransmission;
import customerEnter.Frame.Enum.FrameDataSqlDMLUse;
import customerEnter.Frame.JDBCDao.Interface.DaoFrameUseTypeInterface;
import customerEnter.Frame.JDBCDao.Interface.SqlExistenceResult;
import customerEnter.Frame.JDBCDao.Proxy.FrameInvocationHandler;
import customerEnter.Frame.JDBCDao.util.DaoFrameConntroller;
import customerEnter.Frame.Utils.Log4j;
import customerEnter.Frame.daoTransaction.IcyDataTemplateFactory;

@SuppressWarnings({ "rawtypes", "unchecked" })
public class SqlExistenceResultObject<Y> implements SqlExistenceResult {

	@Override
	public <T, K> void execuesResult(DaoFramePameTransmission<T, K> daoFramePameTransmission,
			FrameInvocationHandler frameInvocationHandler, IcyDataTemplateFactory icyDataTemplateFactory) {
		/**
		 * 這裡使用代理會造成多線程 的資源問題
		 */
		DaoFrameUseTypeInterface daoFrameUseTypeInterface = getDaoFrameUseTypeInterface(daoFramePameTransmission
				.getFrameDataSqlDMLUse());
		// Connection connection = null;
		// PreparedStatement preparedStatement = null;
		try {
			// connection = icyDataTemplateFactory.getConnection();
			daoFramePameTransmission.sqlRowSet = icyDataTemplateFactory.getIcyDataTemplate().queryForRowSet(
					daoFramePameTransmission.SQL);
			// preparedStatement =
			// connection.prepareStatement(daoFramePameTransmission.SQL);
			// Log4j.Log4jOutInfo(daoFramePameTransmission.frameDataSqlDMLUse +
			// ":" + daoFramePameTransmission.SQL);
			// daoFramePameTransmission.setRs(preparedStatement.executeQuery());
		} catch (Exception e) {
			Log4j.Log4JOutError("執行查詢失敗" + e.getMessage());
		}
		Object object = daoFrameUseTypeInterface.useDaoFrameDataType(daoFramePameTransmission);
		// System.out.println(getClass().getName());
		// try {
		// DaoFrameConntroller.closeConn(daoFramePameTransmission.getRs(),
		// preparedStatement, connection);
		// } catch (FramErrorProcessing e) {
		// Log4j.setLog4j(getClass());
		// Log4j.Log4JOutError("報錯 關閉鏈接" + e.getMessage());
		// }

		daoFramePameTransmission.getPagingParametersResult().setpObject(object);
	}

	public DaoFrameUseTypeInterface getDaoFrameUseTypeInterface(FrameDataSqlDMLUse frameDataSqlDMLUse) {
		return DaoFrameConntroller.getDaoFrameUseTypeInterface(frameDataSqlDMLUse.getValue().getName());
	}

}
